<?php
/**
 * Created by PhpStorm.
 * User: jcaskrelin
 * Date: 15/9/9
 * Time: 下午11:17
 */

namespace Common\Api;

class Ali_ossApi {

    private $ossClient;

    public function __construct($isInternal = false)
    {
        import('Vendor.AliyunOSS.AliyunOSS');
        $OSS_CONFIG = C('OSS_CONFIG');
        $serverAddress = $isInternal ? $OSS_CONFIG['OSS_ENDPOINT_INT'] : $OSS_CONFIG['OSS_ENDPOINT'];
        $this->ossClient = \AliyunOSS::boot(
            $serverAddress,
            $OSS_CONFIG['OSS_ACCESS_ID'],
            $OSS_CONFIG['OSS_ACCESS_KEY']
        );
    }

    /**
     * @param $ossKey 文件命名，可直接 文件夹/文件夹/文件名 (aa/bb/file.txt)
     * @param $filePath 上传路径
     */
    public static function upload($ossKey, $filePath)
    {
//        $oss = new Ali_ossApi(true); // 上传文件使用内网，免流量费
        $oss = new Ali_ossApi(); // 上线时使用true
        $oss->ossClient->setBucket('sdxectouch');
        $oss->ossClient->uploadFile($ossKey, $filePath);
    }
    /**
     * 直接把变量内容上传到oss
     * @param $osskey 文件命名，可直接 文件夹/文件夹/文件名 (aa/bb/file.txt)
     * @param $content 文件内容
     */
    public static function uploadContent($osskey,$content)
    {
//        $oss = new Ali_ossApi(true); // 上传文件使用内网，免流量费
        $oss = new Ali_ossApi();  // 上线时使用true
        $oss->ossClient->setBucket('sdxectouch');
        $oss->ossClient->uploadContent($osskey,$content);
    }

    /**
     * @param $ossKey 获取文件名，此文件名为自己已知，如：(aa/bb/file.txt)
     * @return mixed
     */
    public static function getUrl($ossKey)
    {
        $oss = new Ali_ossApi();
        $oss->ossClient->setBucket('sdxectouch');
        return $oss->ossClient->getUrl($ossKey, new \DateTime("+1 day"));
    }

    public static function createBucket($bucketName)
    {
        $oss = new Ali_ossApi();
        return $oss->ossClient->createBucket($bucketName);
    }

    public static function getAllObjectKey($bucketName)
    {
        $oss = new Ali_ossApi();
        return $oss->ossClient->getAllObjectKey($bucketName);
    }

}